package com.lysj.admin.master.bussiness.company.company.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lysj.admin.master.bussiness.company.company.domain.CommissionDetail;
import com.lysj.admin.master.bussiness.company.company.domain.Employee;
import com.lysj.admin.master.bussiness.company.company.dto.CommissionDetailQueryDto;
import com.lysj.admin.master.bussiness.company.company.mapper.CommissionDetailMapper;
import com.lysj.admin.master.bussiness.company.company.service.CommissionDetailService;
import com.lysj.admin.master.bussiness.company.company.service.EmployeeService;
import com.lysj.admin.master.bussiness.company.company.vo.EmployeeCommissionVO;
import com.lysj.admin.master.common.annotation.MyList;
import com.lysj.admin.master.common.annotation.TokenInfo;
import com.lysj.admin.master.common.spring.mpbase.MyController;
import com.lysj.admin.master.common.spring.mpbase.MyService;
import com.lysj.admin.master.common.spring.pagination.PageRequestDTO;
import com.lysj.admin.master.common.web.ParamUtil;
import com.lysj.admin.master.common.web.Resp;
import com.lysj.admin.master.common.web.RespUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Created by drj on 2020-01-13 15:28
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/company/commission_detail")
public class CommissionDetailController extends MyController<CommissionDetailMapper, CommissionDetail, CommissionDetailQueryDto> {

    @Resource
    private CommissionDetailService commissionDetailService;
    @Resource
    private EmployeeService employeeService;

    @Override
    public MyService<CommissionDetailMapper, CommissionDetail> getService() {
        return commissionDetailService;
    }

    @MyList
    public Resp list(CommissionDetailQueryDto condition) {
        return super.list(condition);
    }

    @GetMapping("/unaudited_list")
    public Resp unauditedList(PageRequestDTO pageRequestDTO){
        Page<EmployeeCommissionVO> voList = commissionDetailService.findCommissionDetailList(CommissionDetail.CommissionStatus.NO_AUDIT.getCode(),pageRequestDTO);
        return RespUtil.success(voList);
    }

    /**
     * @author Created by zk on 2020/1/15 11:04
     * @Description 员工任务列表
     */
    @GetMapping("/employee_commission_list")
    public Resp employeeCommissionList(@TokenInfo String employeeId, Integer commissionStatus) {
        List<EmployeeCommissionVO> list = commissionDetailService.findEmployeeCommissionList(employeeId, commissionStatus);
        return RespUtil.success(list);
    }

    /**
     * @author Created by zk on 2020/1/15 11:04
     * @Description 员工任务详情
     */
    @GetMapping("/employee_commission_detail")
    public Resp employeeCommissionDetail(String detailId) {
        EmployeeCommissionVO res = commissionDetailService.employeeCommissionDetail(detailId);
        if (res == null) {
            return RespUtil.error(Resp.ResStatus.PARAM_ERROR, "该任务不存在");
        }
        return RespUtil.success(res);
    }

    /**
     * @author Created by zk on 2020/1/15 11:05
     * @Description 接单
     */
    @PostMapping("/receive_order")
    public Resp receiveOrder(String detailId, String signId) {
        if (ParamUtil.isBlank(detailId) || ParamUtil.isBlank(signId)) {
            return RespUtil.error(Resp.ResStatus.PARAM_ERROR, "缺失任务信息或签名信息");
        }
        CommissionDetail commissionDetail = commissionDetailService.getById(detailId);
        if (commissionDetail == null) {
            return RespUtil.error(Resp.ResStatus.PARAM_ERROR, "该任务不存在");
        }
        if(!CommissionDetail.CommissionStatus.NO_RECEIVE.getCode().equals(commissionDetail.getCommissionStatus())){
            return RespUtil.error(Resp.ResStatus.PARAM_ERROR, "该任务非待接单状态");
        }
        commissionDetail.setSignId(signId);
        commissionDetail.setCommissionStatus(CommissionDetail.CommissionStatus.RECEIVE.getCode());
        commissionDetailService.updateById(commissionDetail);
        return RespUtil.success("接单成功");
    }

    /**
     * @author Created by zk on 2020/1/15 11:53
     * @Description 上传凭证
     */
    @PostMapping("/upload_proof")
    public Resp uploadProof(String detailId, String proof) {
        if (ParamUtil.isBlank(detailId) || ParamUtil.isBlank(proof)) {
            return RespUtil.error(Resp.ResStatus.PARAM_ERROR, "缺失任务信息或凭证信息");
        }
        CommissionDetail commissionDetail = commissionDetailService.getById(detailId);
        if (commissionDetail == null) {
            return RespUtil.error(Resp.ResStatus.PARAM_ERROR, "该任务不存在");
        }
        if(!CommissionDetail.CommissionStatus.RECEIVE.getCode().equals(commissionDetail.getCommissionStatus())){
            return RespUtil.error(Resp.ResStatus.PARAM_ERROR, "该任务非已接单状态");
        }
        commissionDetail.setProof(proof);
        commissionDetail.setCommissionStatus(CommissionDetail.CommissionStatus.NO_AUDIT.getCode());
        commissionDetailService.updateById(commissionDetail);
        return RespUtil.success("提交成功");
    }

    /**
     * @author Created by zk on 2020/1/15 11:53
     * @Description 审核通过
     */
    @PostMapping("/finish_commission")
    public Resp finishCommission(String detailId){
        commissionDetailService.finishCommission(detailId);
        return RespUtil.success("审核成功");
    }

    /**
     * @author Created by zk on 2020/1/15 11:53
     * @Description 驳回
     */
    @PostMapping("/reject_commission")
    public Resp rejectCommission(String detailId,String reason){
        if (ParamUtil.isBlank(detailId)) {
            return RespUtil.error(Resp.ResStatus.PARAM_ERROR, "缺失任务信息");
        }
        CommissionDetail commissionDetail = commissionDetailService.getById(detailId);
        if (commissionDetail == null) {
            return RespUtil.error(Resp.ResStatus.PARAM_ERROR, "该任务不存在");
        }
        if(!CommissionDetail.CommissionStatus.NO_AUDIT.getCode().equals(commissionDetail.getCommissionStatus())){
            return RespUtil.error(Resp.ResStatus.PARAM_ERROR, "该任务非待审核状态");
        }
        commissionDetail.setCommissionStatus(CommissionDetail.CommissionStatus.RECEIVE.getCode());
        commissionDetail.setReason(reason);
        commissionDetailService.updateById(commissionDetail);
        return RespUtil.success("驳回成功");
    }

}
